<template>
  <div>
    <!-- 订货单查询-->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :isSelectColumn="false"
        columnsRef="zjOrderFormQuerys"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle()">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="detailVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="isAccountProject" :title="'大客户项目信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AccountProject :rowData="formData" @close="ProjectClose" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, exportList, getNotPage, getDeptList, getOfficeList,exporturl } from '@/api/omSale/OrderManagement/PurchaseOrderQuery/index.js';
import detailModel from './detailModel';
import AccountProject from './AccountProject';
import { exportExcelFile } from '@/utils';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'acknowledgementOrder',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        searchType: 4,
        majorType: 3,
        saveType: 'DLR'
      },
      fetchApi: getList,
      formData: {},
      exportLoading: false,
      issearch: false,
      detailVisible: false,
      isAccountProject: false,
      ProjectId: ''
    };
  },
  components: {
    detailModel,
    AccountProject
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: this.createDictList('6043')
        },
        {
          type: 'INPUT',
          label: '订货单编号',
          fieldName: 'orderNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('6006'),
          change: p => {
            if (p) {
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
              this.getNotPage(p);
            } else {
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
            }
          }
        },
        {
          type: 'SELECT',
          label: '订单类型细分',
          fieldName: 'orderDetailType',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'startDate|endDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'RANGE_DATE',
          label: '提交日期',
          fieldName: 'reportStartDate|reportEndDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT',
          label: '大客户项目名称',
          fieldName: 'majorProjectName',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          change: p => {
            if (!p) {
              this.ProjectId = '';
            }
          }
        },
        {
          type: 'SELECT',
          label: '是否大客户订单',
          fieldName: 'majorType',
          placeholder: '请选择',
          itemList: [
            { text: '是', value: 2 },
            { text: '否', value: 1 }
          ]
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  详情
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '订货单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '6006')}</span>;
          }
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '订单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6043'),
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '6043')}</span>;
          }
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户项目编号',
          dataIndex: 'projectCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户项目名称',
          dataIndex: 'majorProjectName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户名称',
          dataIndex: 'majorCustomerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6004'),
          render: p => {
            return <span>{this.createDictText(p.row.transportType, '6004')}</span>;
          }
        },
        {
          title: '提报数量',
          dataIndex: 'orderTotalNum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '要求交付日期',
          dataIndex: 'requiredDeliveryDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '订单总金额',
          dataIndex: 'beforeTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣总金额',
          dataIndex: 'discountTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '返利使用总金额',
          dataIndex: 'rebateTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单车返利总金额',
          dataIndex: 'singleRebateTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '建店返利总金额',
          dataIndex: 'buildRebateTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '实付总金额',
          dataIndex: 'realPayTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '审批数量',
          dataIndex: 'checkNum',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{p.row.checkNum ? p.row.checkNum : 0}</span>;
          }
        },
        {
          title: '发运数量',
          dataIndex: 'shipmentNum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '审核时间',
          dataIndex: 'auditTime',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '创建人',
          dataIndex: 'createdName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    selectChangeHandle(row) {},
    seacherFun(type) {
      this.isAccountProject = true;
    },
    async addHandle() {
      let json = Object.assign({}, { CODE: 'CODE_0000057' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }




      // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     let json = this.fetchParams;
      //     this.exportLoading = true;

      //     exportList(json)
      //       .then(response => {
      //         console.log(response, 'response');
      //         exportExcelFile(response.data, '订货单查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     console.log('456');
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      this.fetchParams.projectCode = this.ProjectId;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.detailVisible = true;
    },
    emitcloseHandler() {
      this.detailVisible = false;
    },
    ProjectClose(val) {
      if (val) {
        this.topFilterList.map(x => {
          if (x.fieldName == 'majorProjectName') {
            x.initialValue = val.projectName;
            this.ProjectId = val.projectCode;
          }
        });
      }
      this.isAccountProject = false;
    },
    async getNotPage(orderType) {
      let list = await getNotPage({ orderType: orderType });
      this.topFilterList.map(x => {
        if (x.fieldName == 'orderDetailType') {
          x.itemList = list.data.map(x => ({ text: x.subName, value: x.subId }));
        }
      });
    },
    dealercloseHandler(val) {
      if (val) {
        this.topFilterList.find(x => x.fieldName == 'dealerName').initialValue = val.dealerName;
        this.dealerCode = val.dealerCode;
      }
      this.issearch = false;
    },
    resetChange(val) {
      this.ProjectId = '';
      this.fetchParams.projectCode = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
